package vn.aptech.scrumgroup.model;

import java.util.ArrayList;
import java.util.List;

import vn.aptech.scrumgroup.event.MoveRoundEvent;
import vn.aptech.scrumgroup.event.MoveRoundListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import demo.axitdn.physic.Unit;
import demo.axitdn.physic.Vector3f;

public class CircleOnCircleModel extends CircleWithTextModel implements OnCircle{
	
	List<MoveRoundListener> moveObservers = new ArrayList<MoveRoundListener>();
	private CircleWithTextModel motherCircle;
	private float angle;
	private float aboveAmount;
	private boolean hasBorder = true;
	private int borderColor;
	
	public CircleOnCircleModel(int backColor, int foreColor, int borderColor) {
		super(backColor, foreColor);
		this.borderColor = borderColor;
		// TODO Auto-generated constructor stub
	}
	
	public CircleOnCircleModel(CircleWithTextModel motherCircle, float aboveAmount, float angle, float radius,
			boolean drawOutline, float outlineWidth, String text,float charWidth, int backColor, int foreColor, int borderColor) {
		super(backColor, foreColor);
		this.borderColor = borderColor;
		setProperties(motherCircle, aboveAmount, angle, radius, drawOutline, outlineWidth, text, charWidth);
	}
	
	public CircleOnCircleModel(CircleWithTextModel motherCircle, float aboveAmount, float angle, float radius,
			boolean drawOutline, float outlineWidth, String text,
			float charWidth, boolean hasBorder, int backColor, int foreColor, int borderColor) {
		super(backColor, foreColor);
		this.borderColor = borderColor;
		setProperties(motherCircle, aboveAmount, angle, radius, drawOutline, outlineWidth, text, charWidth);
		this.hasBorder = hasBorder;
	}

	public void setProperties(CircleWithTextModel motherCircle, float aboveAmount, float angle, float radius,
			boolean drawOutline, float outlineWidth, String text,
			float charWidth) {
		float cx = (float) (motherCircle.getCx()+(motherCircle.getRadius()+aboveAmount)*Math.cos(angle));
		float cy = (float) (motherCircle.getCy()+(motherCircle.getRadius()+aboveAmount)*Math.sin(angle));
		setProperties(cx, cy, radius, drawOutline, outlineWidth, text, charWidth);
		// TODO Auto-generated constructor stub
		this.motherCircle = motherCircle;
		this.angle = angle;
		this.aboveAmount = aboveAmount;
	}
	
	public void setCircle(CircleWithTextModel model) {
		// TODO Auto-generated method stub
		motherCircle = model;
	}

	public CircleWithTextModel getCircle() {
		// TODO Auto-generated method stub
		return motherCircle;
	}
	
	@Override
	public void setContext(Context context) {
		// TODO Auto-generated method stub
		super.setContext(context);
		motherCircle.setContext(context);
	}
	
	public void moveRoundOnCircle(float angle) {
		float alpha = this.angle+angle;
		if (alpha>Math.PI)
			alpha = (float) (alpha - 2*Math.PI);
		if (alpha<-Math.PI)
			alpha = (float) (alpha+2*Math.PI);
		setProperties(motherCircle, aboveAmount, alpha, radius, drawOutline, outlineWidth, text, charWidth);
	}

	public void actionPerformed(MoveRoundEvent evt) {
		// TODO Auto-generated method stub
		moveRoundOnCircle(evt.getAngle());
	}
	
//	@Override
	public void moveTo(int x, int y) {
		// TODO Auto-generated method stub
		//super.onDragged(x, y);
		Vector3f v1 = new Vector3f(this.getCx()-motherCircle.getCx(), this.getCy()-motherCircle.getCy(),0);
		Vector3f v2 = new Vector3f(x - motherCircle.getCx(), y - motherCircle.getCy(), 0);
		float alpha = v1.angleBetween(v2, true);
		moveRoundOnCircle(alpha);
		fireMoveRoundEvent(new MoveRoundEvent(alpha));
	}
//	
//	@Override
//	public boolean onPressed(int x, int y) {
//		// TODO Auto-generated method stub
//		return super.onPressed(x, y);
//	}
	
	public void addMoveRoundListener(MoveRoundListener listener){
		moveObservers.add(listener);
	}
	
	public void fireMoveRoundEvent(MoveRoundEvent evt){
		for (MoveRoundListener ml:moveObservers){
			if (ml instanceof TextOnArcModel){
				TextOnArcModel tam = (TextOnArcModel)ml;
				if (tam.getText()=="Transparency"||tam.getText()=="Inspection"||tam.getText()=="Adaptation")
					ml.actionPerformed(new MoveRoundEvent(-evt.getAngle()));
				else
					ml.actionPerformed(evt);
			}else
				ml.actionPerformed(evt);
		}//end for
	}//end method
	
	@Override
	public void onDraw(Canvas c) {
		// TODO Auto-generated method stub
		Paint p = new Paint();
		p.setAntiAlias(true);		
		//p.setARGB(255, 107, 140, 36);//green
		if(borderColor == -1){
			p.setColor(backColor);
		}else{
			p.setColor(borderColor);
		}
		
		if (hasBorder)
			c.drawCircle(this.getCx(), this.getCy(), this.getRadius()+ 9*Unit.i,p);
		super.onDraw(c);
	}//end onDraw 
}
